<template>
  <div class='channelEdit'>
    <van-cell :border="false" center>
      <div slot="title" class="channel-title">我的频道</div>
      <van-button
        type="danger"
        plain
        round
        size="mini"
        @click="onChangeEditBtnShow"
      >
        {{ isEditChannel ? '完成' : '编辑' }}
      </van-button>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="(channel, index) in userChannels"
        :class="{ active: index === active }"
        :icon="(isEditChannel && index !== 0) ? 'clear' : ''"
        :key="channel.id"
        :text="channel.name"
        @click="onUserChannelClick(channel, index)"
      />
    </van-grid>

    <van-cell :border="false" center class="channelRecommend">
      <div slot="title" class="channel-title">频道推荐</div>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        class="grid-item"
        v-for="channel in recommendAllchannels"
        :key="channel.id"
        :text="channel.name"
        @click="onAddChannel(channel)"
      />
    </van-grid>
  </div>
</template>

<script>
import { getAllChannels, setUserChannel, deleteUserChannelApi } from '@/api/user'
import { setStorage } from '@/utils/storage'
import { mapState } from 'vuex'

export default {
  name: 'channelEdit',
  components: {},
  props: {
    userChannels: {
      type: Array,
      required: true
    },
    active: {
      type: Number,
      required: true
    }
  },
  data () {
    return {
      allChannels: [],
      isEditChannel: false
    }
  },
  computed: {
    // 计算频道推荐的列表，方式：所有频道列表 - 我的频道列表 = 频道推荐列表
    recommendAllchannels () {
      return this.allChannels.filter(channels => {
        return !this.userChannels.find(ele => {
          return channels.id === ele.id
        })
      })
    },
    ...mapState(['user'])
  },
  created () {
    this.loadAllChannels()
  },
  methods: {
    async loadAllChannels () {
      const data = await getAllChannels()
      this.allChannels = data.data.data.channels
    },
    async onAddChannel (channel) {
      this.userChannels.push(channel)
      if (this.user) {
        // 如果登录则线上存储
        await setUserChannel({
          channels: [{ id: channel.id, seq: this.userChannels.length }]
        })
      } else {
        // 未登录则本地存储
        setStorage('localStoragesUserChannels', this.userChannels)
      }
    },
    onChangeEditBtnShow () {
      this.isEditChannel = !this.isEditChannel
    },
    onUserChannelClick (channel, index) {
      if (this.isEditChannel && index !== 0) {
        // 如果为 true 证明为编辑状态，点击后执行删除频道。
        this.deleteUserChannel(channel, index)
      } else {
        // 如果为 false 证明为非编辑状态，点击后切换频道。
        this.changeUserChannel(index)
      }
    },
    async deleteUserChannel (channel, index) {
      this.userChannels.splice(index, 1)
      // 如果删除的是当前激活频道之前的频道
      if (index < this.active) {
        this.$emit('updateActive', this.active - 1)
      }
      if (this.user) {
        // 如果登录则线上存储
        await deleteUserChannelApi(channel.id)
      } else {
        // 未登录则本地存储
        setStorage('localStoragesUserChannels', this.userChannels)
      }
    },
    changeUserChannel (index) {
      this.$emit('closePopup')
      this.$emit('updateActive', index)
    }
  }
}
</script>
<style lang='less' scoped>
.channelEdit{
  position: fixed;
  top: 54px;
  bottom: 0;
  right: 0;
  left: 0;
  overflow-y: auto;
  .channel-title {
    font-size: 16px;
    color: #333333;
  }
  .channelRecommend {
    margin-top: 20px;
  }
  .van-button--mini {
    padding: 0px 14px;
  }
  .grid-item {
    width: 80px;
    height: 43px;
    text-align: center;
    /deep/ .van-grid-item__content {
      background-color: #f4f5f6;
      .van-grid-item__text {
        font-size: 14px;
        color: #222222;
        margin-top: 0;
      }
    }
    /deep/ .van-grid-item__icon {
      position: absolute;
      right: -10px;
      top: -7px;
      font-size: 20px;
      color: #ccc;
    }
  }
  .active {
    /deep/ .van-grid-item__text {
      color: red !important;
    }
  }
}
</style>
